<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Repositories\DepartmentRepository;
use App\Http\Requests\DepartmentRequest;
use App\Repositories\UserRepository;
use App\Services\PHPTree;

class DepartmentController extends AdminController
{
    public function __construct(DepartmentRepository $department, UserRepository $user)
    {
        $this->department = $department;
        $this->user = $user;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = (new PHPTree($this->department->all()->toArray()))->render();

        $users = $this->user->all()->keyBy('id');

        return view('admin.department', compact('data', 'users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $regions = $this->department->regionLists()->prepend('设置为区域', 0);

        return view('admin.department_add', compact('regions'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(DepartmentRequest $request)
    {
        $result = $this->department->create($request->all());

        if ($result) {
            return back()->with('success', '部门添加成功');
        }

        return back()->with('error', '部门添加失败');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $regions = $this->department->regionLists()->prepend('设置为区域', 0);
        $data = $this->department->find($id);

        return view('admin.department_edit', compact('data', 'regions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(DepartmentRequest $request, $id)
    {
        $result = $this->department->updateFind($request->all(), $id);

        if ($result) {
            return redirect('admin/department')->with('success', '部门信息更新成功'); 
        }

        return back()->with('error', '部门信息更新失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // 检查这个部门或者区域的人数
        $userCount = $this->user->getCountByDepartment($id);

        if ($userCount > 0) {
            flash('error', '部门删除失败，请转移相关人员后再删除。');
            return response()->json(['code' => 500]);
        }

        $result = $this->department->delete($id);

        if ($result) {
            flash('success', '部门删除成功');
            return response()->json(['code' => 200]);
        }

        flash('error', '部门删除失败');
        return response()->json(['code' => 500]);
    }

    // 更新部门主管
    public function updateManager(Request $request, $id)
    {
        $manager =  $request->input('manager');

        $result = $this->department->update(['manager' => $manager], $id);

        if ($result) {
           return back()->with('success', '更新部门主管成功'); 
        }

        return back()->with('error', '更新部门主管失败');
    }
    
}
